package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {


    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyQuery qo){

        //看前端页面共享个page
        // pageinfo :是mybatis插件,操作的是mysql数据库
        // Page  : 是spring-data 的组件,操作的是mongodb
        Page<Strategy> page =  strategyService.query(qo);

        model.addAttribute("page",page);
        return "strategy/list";
    }


    // 添加按钮中保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        // 保存和编辑
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();

    }

    // 攻略编辑功能(编辑操作要回显数据,所以要把数据共享到页面)
    @RequestMapping("/input")
    public String input(Model model,String id){

        if(StringUtils.hasLength(id)){
            // 攻略strategy
            model.addAttribute("strategy",strategyService.get(id));
        }

        // 攻略分类catalogs
        model.addAttribute("catalogs",strategyCatalogService.groupList());

        // 攻略主题themes
        model.addAttribute("themes",strategyThemeService.listAll());

        return "strategy/input";
    }

    // 下架按钮功能(实际上就是改变状态)
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id ,int state ){
        // 下架就是改变状态
        strategyService.changeState(id, state);
        return JsonResult.success();

    }
}
